<template>
  <div>
    <div class="filter-container _formStyle_" style="padding: 10px 0">
      <el-form :inline="true" :model="form" ref="formRef">
        <el-form-item label="feign接口名:">
          <el-input
            v-model.trim="form.feignClientName"
            placeholder="feign接口名"
            clearable
          />
        </el-form-item>
        <el-form-item label="目标项目名:">
          <el-input
            v-model.trim="form.targetProjectName"
            placeholder="目标项目名"
            style="width: 160px"
            clearable
          />
        </el-form-item>
        <el-form-item label="目标接口名:">
          <el-input
            v-model.trim="form.requestName"
            placeholder="目标接口名"
            style="width: 160px"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-form-item>
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="handleFilter"
              >查询</el-button
            >
            <el-button
              class="filter-item"
              style="margin-left: 10px"
              type="primary"
              icon="el-icon-edit-outline"
              @click="handleReset"
              >重置</el-button
            >
          </el-form-item>
        </el-form-item>
      </el-form>
    </div>
    <el-table height="690" :data="list" highlight-current-row>
      <el-table-column
        prop="feignClientName"
        header-align="center"
        align="center"
        label="feign接口名"
      />
      <el-table-column
        prop="targetProjectName"
        header-align="center"
        align="center"
        label="目标项目名"
      />
      <el-table-column
        prop="requestName"
        header-align="center"
        align="center"
        label="目标接口名"
      />
      <el-table-column
        header-align="center"
        align="center"
        label="接口请求参数"
        width="250"
      >
        <template slot-scope="scope">
          <div v-for="(item, index) in scope.row.request_Parms" :key="index">
            参数类型:{{ item.paramsType }}-参数名:{{ item.paramsName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="feign_desc"
        header-align="center"
        align="center"
        label="描述"
        width="200"
      />
      <!-- <el-table-column
        prop="projectName"
        header-align="center"
        align="center"
        label="当前项目名"
      /> -->
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="deleteHandle(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="form.page"
      :limit.sync="form.limit"
      @pagination="getList"
    />
    <el-dialog
      :title="editTitle"
      :visible.sync="dialogVisible"
      width="67%"
      :close-on-click-modal="false"
      v-if="dialogVisible"
    >
      <component
        :is="currentComponent"
        :editDate="editDate"
        @close="handleClose"
      ></component>
    </el-dialog>
  </div>
</template>

<script>
import {
  projectdatebaseLists,
  listFeignInterface,
  projectcustomdelete,
} from "@/api/lowcode";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination 分页组件
export default {
  name: "feignComponents",
  components: {
    Pagination,
  },
  data() {
    return {
      currentComponent: "customQuery",
      editDate: {},
      dialogVisible: false,
      editTitle: "查询组件编辑",
      // 筛选框的 form 表单
      form: {
        feignClientName: "",
        targetProjectName: "",
        requestName: "",
        projectId: +sessionStorage.getItem("developLowCodeId"),
        // page: 1,
        // limit: 20
      },
      list: [], // 表格数据
      total: 0, // 总条数
      importanceOptions: [],
      typeLists: [
        {
          code: "SELECT",
          name: "查询",
        },
        {
          code: "UPDATE",
          name: "更新",
        },
        {
          code: "DELETE",
          name: "删除",
        },
        {
          code: "SQL",
          name: "SQL",
        },
      ],
    };
  },
  // async created() {
  //   this.getList();
  //   this.projectdatebaseLists();
  // },
  //由于该组件添加了keep-alive缓存，所以每次进入这个组件调用activated生命周期
  activated() {
    this.getList();
    this.projectdatebaseLists();
  },
  methods: {
    handleClose(flag) {
      this.dialogVisible = false;
      if (flag) {
        this.getList();
      }
    },
    async projectdatebaseLists() {
      this.importanceOptions = [];
      const res = await projectdatebaseLists(
        +sessionStorage.getItem("developLowCodeId")
      );
      if (res?.code === 200) {
        this.importanceOptions = res.data;
      }
    },
    // 获取表格数据
    async getList() {
      this.list = [];
      // this.total = 0;
      const res = await listFeignInterface(this.form);
      if (res?.code === 200) {
        this.list = JSON.parse(JSON.stringify(res.data));
        this.list.forEach((item) => {
          this.$set(
            item,
            "request_Parms",
            item.requestParams ? JSON.parse(item.requestParams) : []
          );
        });
        // this.total = res.data.totalCount;
      }
    },
    // 重新获取表格数据 页码归为1
    handleFilter() {
      // this.form.page = 1;
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.getList();
        }
      });
    },
    handleReset() {
      this.form = {
        feignClientName: "",
        targetProjectName: "",
        requestName: "",
        projectId: +sessionStorage.getItem("developLowCodeId"),
      };
      this.getList();
    },
    //编辑先不做
    handleUpdate(row) {
      this.dialogVisible = true;
      this.editDate = JSON.parse(JSON.stringify(row));
    },
    async deleteHandle(item) {
      this.$confirm(`确定要进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const res = await projectcustomdelete([item.id]);
        if (res?.code === 200) {
          this.$message({
            //message: res.message,
            message: "删除成功",
            type: "success",
          });
          this.getList();
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "~@/styles/layout-table.scss";
$rowLength: 2;
@media all and (min-width: 1659px) {
  $rowLength: 2;
  @include calculation($rowLength: $rowLength);
}
@media all and (max-width: 1659px) {
  $rowLength: 3;
  @include calculation($rowLength: $rowLength);
}
._formStyle_ ::v-deep .el-form-item__label {
  text-align: left;
  margin-top: 4px;
}
</style>

